/** @odoo-module **/

import { Component } from "@odoo/owl";
import { registry } from "@web/core/registry";
import { standardFieldProps } from "@web/views/fields/standard_field_props";
import { useService } from "@web/core/utils/hooks";

/**
 * 条码扫描按钮 Widget for Production Work Order
 * 显示为一个大按钮，点击时直接打开扫描器
 */
export class BarcodeScanButtonWidget extends Component {
    static template = "production_planning.BarcodeScanButtonWidget";
    static props = {
        ...standardFieldProps,
    };

    setup() {
        super.setup();
        this.orm = useService("orm");
        this.notification = useService("notification");
    }

    /**
     * 点击按钮时触发
     */
    async onClick() {
        console.log("Production barcode scan button clicked");
        
        try {
            // 直接调用全局函数打开扫描器
            if (window.openProductionBarcodeScannerDirect) {
                await window.openProductionBarcodeScannerDirect(this.props.record, this.orm);
            } else {
                console.error("openProductionBarcodeScannerDirect function not found");
                this.notification.add("扫描器功能不可用", { type: "danger" });
            }
            
        } catch (error) {
            console.error("Error opening barcode scanner:", error);
            this.notification.add("打开扫描器时出错：" + error.message, { type: "danger" });
        }
    }
}

// 注册 widget（使用production_前缀避免与my_hostel冲突）
registry.category("fields").add("production_barcode_scan_button_widget", {
    component: BarcodeScanButtonWidget,
    supportedTypes: ["boolean"],
});

